package structure.helpercalss;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * @AUTHOR LYF
 * @DATE 2022/3/18
 * @VERSION 1.0
 * @DESC
 * 计数器案例
 *
 *
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        ArrayList data = new ArrayList(10);
        Collections.addAll(data,"","","","","","","","","","");

        ArrayList[]data2 = new ArrayList[10];


        CountDownLatch countDownLatch = new CountDownLatch(10);// 至少需要六个完成才能停止阻塞算完成任务
        // 并发拿十页数据
        for(int i=0;i<10;i++){
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"进行执行任务---");

                try {
                    Thread.sleep(new Random().nextInt(10));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 模拟数据
                //data.set()

                String name=Thread.currentThread().getName();
                //name.split("\\-");
                data.set(Integer.valueOf(name.substring(7)),name+"组数据");

                System.out.println(Thread.currentThread().getName()+"执行任务---完毕");

                countDownLatch.countDown();
            },"thread-"+i).start();
        }

        countDownLatch.await();
        System.out.println("完成任务...");
        data.forEach(u->{
            System.out.println(u);
        });



    }
}
